/*
 *date: 2016-04-29
 *name: 工具扩展
 *version: v1.4
 *designBy: heng.wang
 *readme: need zepto||jquery
 *api: dialog/基础弹框,alert/封装好的警示框,confirm/封装好的确认框,notice/通知,tips/提示,loader/加载动画,loaded/关闭加载动画,isEmpty/是否空
 */
;(function($){
	 var king = {
		isEmpty: function(info){
			if(info==null||info==undefined||(""+info)==""){
				return true;
			}
			return false;
		},
		dialog: function(data){
				var boxId = "box1990"+(Math.random()+'').substring(2,4);
				var closeId = "close"+boxId;
				var hideBox = function(){
					var box = $("#"+boxId);
					box.removeClass("show");
					setTimeout(function(){
						box.remove();
					},100);
				}
				var data = {
					"title":!data.title?"提示":data.title+'',
					"html":!data.html?"没有内容":data.html+'',
					"btns":!data.btns?[{"text":"确定"}]:data.btns,
					"init":data.init
				};
				var boxHtml = '<div class="king_box" id="'+boxId+'"><div class="king_box_in"><div class="king_box_title">'+data.title+'</div><div class="king_box_cont">'+data.html+'</div><div class="king_box_btns"></div></div></div>';
				$("#"+boxId).remove();
				$("body").append(boxHtml);
				$("#"+closeId).on("click",function(e){
					hideBox();
					e.preventDefault();
				});
				$.each(data.btns,function(i){
					$('<span>'+data.btns[i].text+'</span>').appendTo("#"+boxId+" .king_box_btns").on("click",function(e){
						data.btns[i].ev?(data.btns[i].ev({"hide":function(){hideBox()}})!==false&&hideBox()):hideBox();
					});
				})
				setTimeout(function(){
					$("#"+boxId).addClass("show");
				},13);
				if(data.init)data.init();
		},
		alert: function(data, callback){
			var JSON = {};
			if($.isArray(data)){
				if(data.length==2){
					JSON = {"title":"温馨提示","content":data[0],"btn":data[1]||'确认'}
				}else if(data.length==3){
					JSON = {"title":data[0],"content":data[1],"btn":data[2]||'确认'}
				}
			}else{
				JSON = {"title":"温馨提示","content":data,"btn":"确定"}
			}
			this.dialog({
				"title": JSON.title,
				"html": JSON.content,
				"btns": [{"text":JSON.btn, ev: callback}]
			});
		},
		confirm: function(data,callback1,callback2){
			var JSON = {};
			if($.isArray(data)){
				if(data.length==3){
					JSON = {"title":"温馨提示","content":data[0],"ok":data[1]||'确认',"cancel":data[2]||'取消'}
				}else if(data.length==4){
					JSON = {"title":data[0],"content":data[1],"ok":data[2]||'确认',"cancel":data[3]||'取消'}
				}
			}else{
				JSON = {"title":"温馨提示","content":data,"ok":"确定","cancel":"取消"}
			}
			this.dialog({
				"title":JSON.title,
				"html":JSON.content,
				"btns":[{"text":JSON.ok,ev:callback1},{"text":JSON.cancel,ev:callback2}]
			});
		},
		notice: function(str,title){
			this.dialog({
				"title":title||"提示",
				"html":'<p style="text-align:left;text-indent:2em;">'+str+'</p>',
				"btns": [{"text":"关闭"}]
			});
		},
		tips: function(str,t){
			var timeout1 = false;
			var show = function(str,t){
				var t = t||2000;
				var id = "tips"+(Math.random()+'').substring(2,4);
				var html = '<div class="king_pop_tips" id="'+id+'"><span>'+str+'</span></div>';
				$("body").append(html);
				var ts = $("#"+id);
				if($(".king_pop_tips").size()>0){
					hide(ts.siblings(".king_pop_tips"));
				}
				setTimeout(function(){
					ts.addClass("show");
				},10);
				timeout1 = setTimeout(function(){
					hide(ts);
				},t);
			}
			var hide = function(o){
				var o = o||$(".king_pop_tips");
				clearTimeout(timeout1);
				o.css({"opacity":"0"});
				setTimeout(function(){
					o.remove();
				},300);
			}
			show(str,t);
		},
		loader: function(str){
			if($(".king_ld").size()==0){
				var ldStr = '<div class="king_ld"><div class="king_lds"><span>'+(str||'')+'</span></div></div>';
				$("body").append(ldStr);
			}else{
				$(".king_ld").show();
			}
		},
		loaded: function(){
			$(".king_ld").remove();
		},
		getLocalImage: function(input,callback){
			if(input instanceof HTMLElement){
				var file = input.files[0];
				var reg = /image\/jpg|png|gif|jpeg/;
				if(!file){
					this.tips("请选择图片");
					return false;
				}else if(!reg.test(file.type)){
					this.tips("可选图片格式为png、jpg、gif");
					input.value = '';
					return false;
				}else{
					var reader = new FileReader();
					reader.readAsDataURL(file);
					reader.onload = function(e){
						if(typeof callback === "function"){
							callback({"name":file.name,"type":file.type,"size":file.size,"src":this.result});
						}
					}
				}
			}else{
				this.tips("input参数错误");
			}
		},
		compressImage: function(src, length, callback){
			var lenth = parseInt(lenth);
			var image = new Image();
			image.src = src;
			image.onload = function() {
				var width = image.width;
				var height = image.height;
				var aspect = width/height;
				if(width>=length){
					var w = length;
					var h = w/aspect;
				}else if(height>=length){
					var h = length;
					var w = h*aspect;
				}else{
					return false;
				}
				var canvas = $('<canvas width="'+w+'" height="'+h+'"></canvas>').appendTo("body").get(0);
				var context = canvas.getContext('2d');
				context.drawImage(image, 0, 0, w, h);
				var imgData = canvas.toDataURL("image/png");
				if($.isFunction(callback)){
					document.body.removeChild(canvas);
					callback(imgData);
				}
			};
		},
		viewImage: function(src){
			var boxId = "box1990"+(Math.random()+'').substring(2,4);
			$(".lkt_image_view").size()==0&&$('<div class="lkt_image_view load" id="'+boxId+'"></div>').appendTo("body").on("click", function(){
				$(this).remove();
			});
			var img = new Image();
			img.src = src;
			img.onload = function(){
				$("#"+boxId).append(img).removeClass("load");
			}
		},
		 doAjax: function(data){
			 var _this = this;
			 _this.isEmpty(data.data)&&(data.data = {});
			 data.data._csrf = $('meta[name="csrf-token"]').attr("content");
			 $.ajax({
				 url: data.url,
				 type: data.type||"get",
				 dataType: data.dataType,
				 data: data.data,
				 success: data.success,
				 error: data.error||function(){
					 _this.alert("网络连接失败");
					 _this.loaded();
				 }
			 });
		 },
		go: function(url){
			if(typeof url == "string"){
				window.location.href = url
			}else if(typeof url == "number"){
				window.history.go(url)
			}
		}
	};
	$.extend($, king);
})(typeof Zepto!=="undefined"?Zepto:jQuery);
$(function() {
	//初始化click
	FastClick.attach(document.body);
});
//滚动到指定位置执行回调
function whileToPosition(pos, callback1, callback2){
	$(window).scroll(function(){
		if($(window).scrollTop() >= pos){
			$.isFunction(callback1)&&callback1();
		}else{
			$.isFunction(callback2)&&callback2();
		}
	});
}

//底部加载更多
function whileToEnd(callback){
	$(window).scroll(function(){
		if($(window).scrollTop()>=$(document).height()-$(window).height()){
			$.isFunction(callback)&&callback();
		}
	});
}


//支付倒计时
function dateCountDown(obj,t) {
    if(t > 0) {
        t --;
        $(obj).attr("data",t);
        $(obj).children("span").text(timeFormat(t));
    }
    setTimeout(function(){
        dateCountDown(obj,t);
    },1000);
}

/**
 *方法名称：秒钟倒计时
 *参数：{obj:显示秒数的对象;t:总秒数;callback:结束后的回调函数}
 */
function rTime(obj, t, callback) {
	if(typeof t != "number" && typeof callback != "function"){
		console.info("dtime:参数类型出错!");
		return false;
	}
    if(t > 0){
		$(obj).text(t);
        t--;
		setTimeout(function(){
			rTime(obj,t,callback);
		},1000);
    }else if(typeof callback == "function"){
		callback();
	}
}
/**
 *方法名称：日期倒计时
 *参数：{obj:显示日期的对象;t:总日期;callback:结束后的回调}
 */
function rDate(obj, t, callback){
	if(typeof t != "number" && typeof callback != "function"){
		console.info("dtime:参数类型出错!");
		return false;
	}
    if(t > 0) {
		$(obj).text(timeFormat(t));
        t --;
		setTimeout(function(){
			rDate(obj,t);
		},1000);
    }else if(typeof callback == "function"){
		callback();
	}
}
/**
 *方法名称：时间格式化
 *参数:{t:时间}
 */
function timeFormat(t){
	t = parseInt(t);
	var d = parseInt(t/(24*3600));
	var h = parseInt((t-(d*24*3600))/3600);
	h = h<10?"0"+h:h;
	var m = parseInt((t-(d*24*3600)-h*3600)/60);
	m = m<10?"0"+m:m;
	var s = parseInt(t-(d*24*3600)-h*3600-m*60);
	s = s<10?"0"+s:s;
	return (d==0?"":d+"天 ")+h+":"+m+":"+s;
}

/*返回顶部 Start*/
$(window).scroll(function () {
    var scroll_top = document.body.scrollTop ;
    if (scroll_top > 0) {
        $("#backTop").show();
    }else{
    	$("#backTop").hide();
    }
});
$("#backTop").on("click",function(){  
	document.body.scrollTop = 0;
	$("#backTop").hide();
});  
/*返回顶部 End*/


